﻿using System;
using System.IO;
using System.Windows.Forms;
using Chatclient;
using MessageHandler;
using PluginInterface;
using Msg = MessageHandler.Message;

namespace DataExchangePack
{
	public class DataExchangeFunctionality : IPlugin
	{
		IPluginHost myPluginHost = null;
		string myPluginName = "DataExchangePack";
		string myPluginAuthor = "MessengerTeam";
		string myPluginDescription = "Fügt die Funktionalität des Datenaustauschs hinzu.";
		string myPluginVersion = "1.0.0";
		static FormPrivateChat formPrivateChat;


		Controller controller;

		private System.Windows.Forms.OpenFileDialog openFileDialog;
		private System.Windows.Forms.SaveFileDialog saveFileDialog;

		public void addFunctionality(System.Windows.Forms.Form form)
		{

			formPrivateChat = (FormPrivateChat)form;
			openFileDialog = new OpenFileDialog();
			openFileDialog.RestoreDirectory = true;
			saveFileDialog = new SaveFileDialog();
			saveFileDialog.RestoreDirectory = true;

			Button buttonSendFile = new Button();
			buttonSendFile.Location = new System.Drawing.Point(12, 359);
			buttonSendFile.Name = "button1";
			buttonSendFile.Size = new System.Drawing.Size(85, 23);
			buttonSendFile.TabIndex = 6;
			buttonSendFile.Text = "Datei senden";
			buttonSendFile.UseVisualStyleBackColor = true;
			buttonSendFile.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			buttonSendFile.Click += new System.EventHandler(this.btnSendFile_Click);
			formPrivateChat.Controls.Add(buttonSendFile);
		}

		private void btnSendFile_Click(object sender, EventArgs e)
		{
			Button sndBtn = (Button)sender;
			StartFileTransfer(formPrivateChat);
		}

		public IPluginHost Host
		{
			get
			{
				return myPluginHost;
			}
			set
			{
				myPluginHost = value;
			}
		}

		public string Name
		{
			get { return myPluginName; }
		}

		public string Description
		{
			get { return myPluginDescription; }
		}

		public string Author
		{
			get { return myPluginAuthor; }
		}

		public string Version
		{
			get { return myPluginVersion; }
		}

		public void Initialize()
		{
			// 
		}

		public void Dispose()
		{
			// 
		}

		public void handleMessage(Msg message, object form)
		{
			if (form is FormPrivateChat)
			{
				FormPrivateChat fpc = (FormPrivateChat)form;
				switch (message.messageType)
				{
					case MessageType.FILETRANSFER:
						WriteFile(message, fpc);
						break;
					case MessageType.STARTFILETRANSFER:
						HandleStartFileTransfer(message, fpc);
						break;
				}
			}
		}

		internal void HandleStartFileTransfer(Msg message, FormPrivateChat fpc)
		{
			controller = fpc.GetController();
			StartFileTransferMessageType type = (StartFileTransferMessageType)message.Item;
			string filename = new FileInfo(type.filename).Name;
			if (type.from.Equals(fpc.userInfo))
			{
				if (type.answer)
				{
					controller.SendFile(type.filename, type.answerer, fpc.ChatMembers.UserInfos.ToArray());
					fpc.AppendChatText(type.answerer.ToString(), "hat die Datei " + filename + " angenommen.");
				}
				else
				{
					fpc.AppendChatText(type.answerer.ToString(), "hat die Datei " + filename + " abgelehnt.");
				}
			}
			else
			{
				string chatText = "möchte Ihnen die Datei " + filename + " schicken. ";
				if (MessageBox.Show("Möchten Sie die Datei " + filename + " von " + type.from + " annehmen?", "Dateiübertragung", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
				{
					controller.AnswerStartFileTransfer(message, true);
					fpc.AppendChatText(fpc.userInfo.ToString(), chatText + "Sie haben die Datei angenommen");
				}
				else
				{
					controller.AnswerStartFileTransfer(message, false);
					fpc.AppendChatText(fpc.userInfo.ToString(), chatText + "Sie haben die Datei abgelehnt");
				}
			}
		}

		private void StartFileTransfer(FormPrivateChat fpc)
		{
			controller = formPrivateChat.GetController();
			if (openFileDialog.ShowDialog() == DialogResult.OK)
			{
				FileInfo fi = new FileInfo(openFileDialog.FileName);
				controller.SendStartFileTransfer(openFileDialog.FileName, fpc.ChatMembers.UserInfos.ToArray());
				fpc.AppendChatText(fpc.userInfo.ToString(), "möchte die Datei " + fi.Name + " versenden.");
			}
		}

		internal void WriteFile(Msg message, FormPrivateChat fpc)
		{
			FileTransferMessageType type = (FileTransferMessageType)message.Item;
			FileInfo fi = new FileInfo(type.fileName);
			saveFileDialog.FileName = fi.Name;
			if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				File.WriteAllBytes(saveFileDialog.FileName, type.someObject);
				fpc.AppendChatText(type.from.ToString(), "Datei " + fi.Name + @" vollständig übertragen. Gespeichert unter <file://" + saveFileDialog.FileName + ">");
			}
		}
	}
}
